﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;
namespace QuanLySanXuat.Controllers
{
    public class UserController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly IUserRepository UserRepository;
        private readonly ICompanyRepository companyRepository;
        public UserController(ICommandBus commandBus, IUserRepository UserRepository,ICompanyRepository companyRepository)
        {
            ModelState.Clear();
            this.commandBus = commandBus;
            this.UserRepository = UserRepository;
            this.companyRepository = companyRepository;
        }
        //
        // GET: /User/
        [HttpGet]
        public ActionResult Index()
        {
            var prodcutType = UserRepository.GetAll();
            ViewData["Company"] = companyRepository.GetAll();
            return View(prodcutType.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Create()
        {
            ModelState.Clear();
            return View();
        }
        [HttpPost]
        public ActionResult Save(UserFormModels frm)
        {
            var form = new UserFormModels()
            {
                ID = frm.ID,
                Active=frm.Active,
                Avartar=frm.Avartar,
                CompanyID=frm.CompanyID,
                CreatedDate=DateTime.Now,
                Email=frm.Email,
                FullName=frm.FullName,
                GroupID=frm.GroupID,
                LastLoginDate = DateTime.Now,
                LastPasswordChangedDate = DateTime.Now,
                Password=frm.Password,
                Phone=frm.Phone,
                UserName=frm.UserName
            };
            var command = Mapper.Map<UserFormModels, CreateOrUpdateUserCommand>(form);
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
            if (ModelState.IsValid)
            {
                var result = commandBus.Submit(command);

            }
            var model = UserRepository.GetAll();
            return PartialView("_UserList", model.ToPagedList(1, 10));
        }

        [HttpGet]
        public JsonResult Get(int ID)
        {
            User product = UserRepository.GetById(ID);
            var obj = Mapper.Map<User, UserFormModels>(product);
            return Json(obj, JsonRequestBehavior.AllowGet);
            
        }

        [HttpPost]
        public ActionResult RemoveItems(string[] ProID)
        {
            List<DeleteUserCommand> list = new List<DeleteUserCommand>();
            foreach (var item in ProID)
            {
                var command = new DeleteUserCommand { ID = Convert.ToInt16(item) };
                list.Add(command);
            }
            var result = commandBus.SubmitRange(list);
            var model = UserRepository.GetAll();
            return PartialView("_UserList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Search(string Name)
        {
            IEnumerable<User> model;
            if (String.IsNullOrEmpty(Name))
            {
                model = UserRepository.GetAll();
            }
            else
            {
                model = UserRepository.GetMany(x => (x.FullName.Contains(Name)) || (x.UserName.Contains(Name)));

            }
            return PartialView("_UserList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult Page(int PageID)
        {
            var prodcutType = UserRepository.GetAll();
            return PartialView("_UserList", prodcutType.ToPagedList(1, PageID));
        }
    }
}
